﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework.Content;

namespace Erebos.ZipEngine
{
    class ZipFile
    {
        private Ionic.Zip.ZipFile _file;

        public ZipFile(string filename)
        {
            if (!File.Exists(filename))
                throw new IOException("Zip Datei existiert nicht");

            _file = new Ionic.Zip.ZipFile(filename);
        }

        public string GetZipFile(string filename)
        {
            if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Erebos/tmp/" + filename))
                File.Delete(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Erebos/tmp/" + filename);
            _file[filename].Extract(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Erebos/tmp/");
            return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Erebos/tmp/" + filename;
        }

        public T GetFileAsContent<T>(string asset)
        {
            ContentManager cm = new ContentManager(MainGame.ServiceProvider);
            cm.RootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Erebos/";
            string path = GetZipFile(asset + ".xnb");
            return cm.Load<T>(asset);
        }
    }
}
